/* eslint-disable */
import inherits from 'inherits';

import {getBusinessObject, is} from 'bpmn-js/lib/util/ModelUtil';
import ImplementationTypeHelper from 'bpmn-js-properties-panel/lib/helper/ImplementationTypeHelper'

import PropertiesActivator from 'bpmn-js-properties-panel/lib/PropertiesActivator';
// Require all properties you need from existing providers.
// In this case all available bpmn relevant properties without camunda extensions.
import processProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/ProcessProps';
import eventProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/EventProps';
import linkProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/LinkProps';
import idProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/IdProps';
import nameProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/NameProps';
import executableProps from 'bpmn-js-properties-panel/lib/provider/bpmn/parts/ExecutableProps'

import elementTemplateChooserProps
  from 'bpmn-js-properties-panel/lib/provider/camunda/element-templates/parts/ChooserProps'
import elementTemplateCustomProps
  from 'bpmn-js-properties-panel/lib/provider/camunda/element-templates/parts/CustomProps'
// camunda properties
import serviceTaskDelegateProps from './parts/ServiceTaskDelegateProps'
import versionTag from 'bpmn-js-properties-panel/lib/provider/camunda/parts/VersionTagProps'
import userTaskProps from 'bpmn-js-properties-panel/lib/provider/camunda/parts/UserTaskProps'
import asynchronousContinuationProps
  from 'bpmn-js-properties-panel/lib/provider/camunda/parts/AsynchronousContinuationProps'
import callActivityProps from 'bpmn-js-properties-panel/lib/provider/camunda/parts/CallActivityProps'
import multiInstanceProps from 'bpmn-js-properties-panel/lib/provider/camunda/parts/MultiInstanceLoopProps'
// import conditionalProps from 'bpmn-js-properties-panel/lib/provider/camunda/parts/ConditionalProps'
import scriptProps from 'bpmn-js-properties-panel/lib/provider/camunda/parts/ScriptTaskProps'
import startEventInitiator from 'bpmn-js-properties-panel/lib/provider/camunda/parts/StartEventInitiator'
// properties
// external task configuration
import externalTaskConfiguration
  from 'bpmn-js-properties-panel/lib/provider/camunda/parts/ExternalTaskConfigurationProps'
// candidate starter groups/users
// import candidateStarter from 'bpmn-js-properties-panel/lib/provider/camunda/parts/CandidateStarterProps'
// history time to live
import historyTimeToLive from 'bpmn-js-properties-panel/lib/provider/camunda/parts/HistoryTimeToLiveProps'
// cjc
import approvalMechanismProps from './parts/approvalMechanismProps'
import advancedPermissionsProps from './parts/advancedPermissionsProps'
import taskTypeProps from './parts/taskTypeProps'
import addUserProps from './parts/addUserProps'
import tipsProps from './parts/tipsProps'
import {getNode} from "../bpmnUtil";
// import {getNode} from "../../components/bpmnUtil";

// // job configuration
// import jobConfiguration from 'bpmn-js-properties-panel/lib/provider/camunda/parts/JobConfigurationProps'

// helpers ////////////////////////////////////////

var isExternalTaskPriorityEnabled = function(element) {
  var businessObject = getBusinessObject(element);

  // show only if element is a process, a participant ...
  if (is(element, 'bpmn:Process') || is(element, 'bpmn:Participant') && businessObject.get('processRef')) {
    return true;
  }

  var externalBo = ImplementationTypeHelper.getServiceTaskLikeBusinessObject(element),
      isExternalTask = ImplementationTypeHelper.getImplementationType(externalBo) === 'external';

  // ... or an external task with selected external implementation type
  return !!ImplementationTypeHelper.isExternalCapable(externalBo) && isExternalTask;
};

// var isJobConfigEnabled = function(element) {
//   var businessObject = getBusinessObject(element);

//   if (is(element, 'bpmn:Process') || is(element, 'bpmn:Participant') && businessObject.get('processRef')) {
//     return true;
//   }

//   // async behavior
//   var bo = getBusinessObject(element);
//   if (asyncCapableHelper.isAsyncBefore(bo) || asyncCapableHelper.isAsyncAfter(bo)) {
//     return true;
//   }

//   // timer definition
//   if (is(element, 'bpmn:Event')) {
//     return !!eventDefinitionHelper.getTimerEventDefinition(element);
//   }

//   return false;
// };

var getInputOutputParameterLabel = function(param, translate) {

  if (is(param, 'camunda:InputParameter')) {
    return translate('Input Parameter');
  }

  if (is(param, 'camunda:OutputParameter')) {
    return translate('Output Parameter');
  }

  return '';
};

var getListenerLabel = function(param, translate) {

  if (is(param, 'camunda:ExecutionListener')) {
    return translate('Execution Listener');
  }

  if (is(param, 'camunda:TaskListener')) {
    return translate('Task Listener');
  }

  return '';
};

var PROCESS_KEY_HINT = 'This maps to the process definition key.'
// The general tab contains all bpmn relevant properties.
// The properties are organized in groups.
function createGeneralTabGroups(element, bpmnFactory, elementRegistry, elementTemplates, translate) {

  // refer to target element for external labels
  element = element.labelTarget || element;

  var generalGroup = {
    id: 'general',
    label: translate('General'),
    entries: []
  };

  var idOptions;
  var processOptions;

  if (is(element, 'bpmn:Process')) {
    idOptions = { description: PROCESS_KEY_HINT };
  }

  if (is(element, 'bpmn:Participant')) {
    processOptions = { processIdDescription: PROCESS_KEY_HINT };
  }

  idProps(generalGroup, element, translate, idOptions);
  nameProps(generalGroup, element, translate);
  processProps(generalGroup, element, translate, processOptions);
  versionTag(generalGroup, element, translate);
  executableProps(generalGroup, element, translate);
  elementTemplateChooserProps(generalGroup, element, elementTemplates, translate);

  var customFieldsGroups = elementTemplateCustomProps(element, elementTemplates, bpmnFactory, translate);

  var detailsGroup = {
    id: 'details',
    label: translate('Details'),
    entries: []
  };
  serviceTaskDelegateProps(detailsGroup, element, bpmnFactory, translate);
  userTaskProps(detailsGroup, element, translate);
  scriptProps(detailsGroup, element, bpmnFactory, translate);
  linkProps(detailsGroup, element, translate);
  callActivityProps(detailsGroup, element, bpmnFactory, translate);
  eventProps(detailsGroup, element, bpmnFactory, elementRegistry, translate);
  // conditionalProps(detailsGroup, element, bpmnFactory, translate);
  startEventInitiator(detailsGroup, element, translate); // this must be the last element of the details group!

  var multiInstanceGroup = {
    id: 'multiInstance',
    label: translate('Multi Instance'),
    entries: []
  };
  multiInstanceProps(multiInstanceGroup, element, bpmnFactory, translate);

  var asyncGroup = {
    id : 'async',
    label: translate('Asynchronous Continuations'),
    entries : []
  };
  asynchronousContinuationProps(asyncGroup, element, bpmnFactory, translate);

  // var jobConfigurationGroup = {
  //   id : 'jobConfiguration',
  //   label : translate('Job Configuration'),
  //   entries : [],
  //   enabled: isJobConfigEnabled
  // };
  // jobConfiguration(jobConfigurationGroup, element, bpmnFactory, translate);

  var externalTaskGroup = {
    id : 'externalTaskConfiguration',
    label : translate('External Task Configuration'),
    entries : [],
    enabled: isExternalTaskPriorityEnabled
  };
  externalTaskConfiguration(externalTaskGroup, element, bpmnFactory, translate);


  var candidateStarterGroup = {
    id: 'candidateStarterConfiguration',
    label: translate('Candidate Starter Configuration'),
    entries: []
  };
  // candidateStarter(candidateStarterGroup, element, bpmnFactory, translate);

  var historyTimeToLiveGroup = {
    id: 'historyConfiguration',
    label: translate('History Configuration'),
    entries: []
  };
  historyTimeToLive(historyTimeToLiveGroup, element, bpmnFactory, translate);

  // var documentationGroup = {
  //   id: 'documentation',
  //   label: translate('Documentation'),
  //   entries: []
  // };
  // documentationProps(documentationGroup, element, bpmnFactory, translate);

  var tipsGroup = {
    id: 'tips-title',
    label: '重要提示',
    entries: []
  }

  tipsProps(tipsGroup, element, bpmnFactory, translate)

  var groups = [];
  groups.push(tipsGroup);
  groups.push(generalGroup);
  customFieldsGroups.forEach(function(group) {
    groups.push(group);
  });
  groups.push(detailsGroup);
  // groups.push(externalTaskGroup);
  groups.push(multiInstanceGroup);
  groups.push(asyncGroup);
  // groups.push(jobConfigurationGroup);
  // groups.push(candidateStarterGroup);
  // groups.push(historyTimeToLiveGroup);
  // groups.push(documentationGroup);

  return groups;
}


// function createExtensionElementsGroups(element, bpmnFactory, elementRegistry, translate) {

//   var propertiesGroup = {
//     id : 'extensionElements-properties',
//     label: translate('Properties'),
//     entries: []
//   };
//   properties(propertiesGroup, element, bpmnFactory, translate);

//   return [
//     propertiesGroup
//   ];
// }






function createCjcTabsGroups(element, bpmnFactory, elementRegistry, translate) {
  var addUser = {
    id: 'addUser-titel',
    label: '添加相关人员',
    entries: []
  }

  var approvalMechanism = {
    id: 'approvalMechanism-title',
    label: translate('approvalMechanismTitle'),
    entries: []
  }

  var advancedPermissions = {
    id: 'advancedPermissions-title',
    label: translate('advancedPermissionsTitle'),
    entries: []
  }

  var taskTypeSetting = {
    id: 'taskType-title',
    label: translate('taskTypeTitle'),
    entries: []
  }

  approvalMechanismProps(approvalMechanism, element, bpmnFactory, translate)
  advancedPermissionsProps(advancedPermissions, element, bpmnFactory, translate)
  taskTypeProps(taskTypeSetting, element, bpmnFactory, translate)
  addUserProps(addUser, element, bpmnFactory, translate)
  return [approvalMechanism, advancedPermissions, taskTypeSetting, addUser]
}


function cjcPropertiesProvider(eventBus, bpmnFactory, elementRegistry, elementTemplates, translate) {

  PropertiesActivator.call(this, eventBus);

  this.getTabs = function(element) {

    console.log(getNode(element));

    var generalTab = {
      id: 'general',
      label: translate('General'),
      groups: createGeneralTabGroups(element, bpmnFactory, elementRegistry, elementTemplates, translate)
    };

    // // 拓展选项卡
    // var extensionsTab = {
    //   id: 'extensionElements',
    //   label: translate('Extensions'),
    //   groups: createExtensionElementsGroups(element, bpmnFactory, elementRegistry, translate)
    // };

    var cjcPropTab = {
      id: 'cjcTabs',
      label: translate('cjcTabs'),
      groups: createCjcTabsGroups(element, bpmnFactory, elementRegistry, translate)
    }

    // Show general + "cjc" tab
    return [generalTab, cjcPropTab];
  };
}

cjcPropertiesProvider.$inject = ['eventBus', 'bpmnFactory', 'elementRegistry', 'elementTemplates', 'translate'];
inherits(cjcPropertiesProvider, PropertiesActivator);
export default cjcPropertiesProvider;